package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    AddressBookService addressBookService;


    /**
     * 列表查询
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list(){

        //创建分页构造器
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        //进行排序
        queryWrapper.orderByDesc(AddressBook::getIsDefault);
        //进行排列
        List<AddressBook> list = addressBookService.list(queryWrapper);

        return R.success(list);
    }

    /**
     * 添加
     * @param addressBook
     * @return
     */

    @PostMapping
    public R<String> add(@RequestBody AddressBook addressBook){
        //设置用户id
        addressBook.setUserId(BaseContext.getCurrentId());
        //直接添加
        addressBookService.save(addressBook);

        return R.success("ok");

    }

    /**
     * 回显数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook>getById(@PathVariable Long id){

        AddressBook byId = addressBookService.getById(id);

        return R.success(byId);

    }

    /**
     * 修改
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<String>update(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);

        return R.success("修改成功");

    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String>delete(@RequestParam Long ids){
        addressBookService.removeById(ids);
        return R.success("删除成功");
    }

    /**
     * 设置默认地址
     * @param id
     * @return
     */
    @PutMapping("/default")
    public R<String> setDefaultAddressApi(@RequestBody AddressBook id){
       // String s = id.get("id").toString();
        String s =id.getId().toString();
                LambdaUpdateWrapper<AddressBook> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        queryWrapper.set(AddressBook::getIsDefault,0);
        boolean update = addressBookService.update(queryWrapper);

        System.out.println(update);
        System.out.println(s);

        AddressBook byId = addressBookService.getById(s);
        byId.setIsDefault(1);
        boolean b = addressBookService.updateById(byId);
        System.out.println(b);
        return R.success("ok");
    }


    /**
     * 选择默认地址
     * @return
     */
    @GetMapping("/default")
    public R<AddressBook> getDefaultAddressApi(){

        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        queryWrapper.eq(AddressBook::getIsDefault,1);
        AddressBook one = addressBookService.getOne(queryWrapper);

        return R.success(one);



    }

}
